#!/bin/sh

# Copyright 1996,1997,1998 Michael Graff <explorer@flame.org>
# You may distribute this file freely providing this notice
# remains intact. 
#
# Modified May 2004 by Rodrigo   
# Changed the Version, August 2013 by Sephiroth (coding@xivservers.net)
#

IRCD_VERSION="Astral-8.0.0a"
CONF_DATE=`date`
LAST_VERSION="Astral-0.0.0"
#
trap "" 13 14 15
MV=mv
RM=rm
SETUP=include/setup.h
OPTIONS_H=include/options.h
OPTIONS=Options
MAKEINST=.install
AUTO_CONFIG=""
#
GLIBCCHECK=undef
STDLIBH=undef
STDDEFH=undef
SYSSYSLOGH=undef
PARAMH=undef
UNISTDH=undef
STRINGH=undef
STRINGSH=undef
STRCASECMP=undef
RUSAGEH=undef
HINDEX=undef
STRERROR=undef
STRTOKEN=define
STRTOK=define
INETADDR=define
INETNTOA=define
INETNETOF=define
GETTIMEOFDAY=undef
LRAND48=undef
STRTOUL=undef
LOCALD=define
NEEDSKIPNAME=""
CCPATH=''
HAVE_BZERO=undef
GETRUSAGE_2=undef
TIMES_2=undef
POSIX_NBLOCK=undef
BSD_NBLOCK=undef
POSIX_SIGNAL=undef
BSD_SIGNAL=undef
TMP=/tmp/.Configtmp$$.c
EXEC=/tmp/.Configtmp$$
PLATE=/tmp/.ConPlate$$
c=''
n=''
#
# Some reasonable defaults
#
DEFOPT="-O"
DEFCFLAGS="$DEFOPT"
DEFLIBS="none"
OSNAME="Could not determine Operating System"
#
CONTACT_URL="http://www.xivservers.net/irc.php"
CONTACT_EMAIL="ircd@xivservers.net"
SERVICES_NAME="services.XIVservers.net"
KLINE_ADDRESS=""
ZIP_LINKS="1"
DPATH="$HOME/astral"
SPATH="$HOME/astral/ircd"
HUB=""
CRYPT_OPER_PASSWORD="1"
CRYPT_ILINE_PASSWORD=$CRYPT_OPER_PASSWORD
MAXCONNECTIONS="512"
PARANOIA="1"

#
# load $OPTIONS if present
#
if [ -r "$OPTIONS" ] ; then
    . $OPTIONS
fi

#
2>/dev/null
if [ "`eval echo -n 'a'`" = "-n a" ] ; then
	c='\c'
else
	n='-n'
fi

case $# in
  1 )
    case "z${1}" in
        z--help | z-h )
            echo "  -n          Use the Options file from a previous setup"
            echo " "
            echo "  --help      display this help and exit"
            echo "  --version   output version information and exit"
            echo " "
            exit 0 ;;
        z--version )
            echo "Config $IRCD_VERSION"; exit 0 ;;
        z-n )
            if [ "$LAST_VERSION" != "$IRCD_VERSION" ] ; then
                echo "You specified the no-questions-asked configure, but the version"
                echo "of Config which created your Options file was LAST_VERSION,"
                echo "And the current version is $IRCD_VERSION."
                echo " "
                echo "Please read the prompts carefully since some of them may have" 
                echo "changed."
                echo " "
            else
                AUTO_CONFIG=Yes
            fi ;;
        * ) ;;
    esac
    ;;
  * ) ;;
esac

if [ "$LAST_VERSION" != "$IRCD_VERSION" ] ; then
    if [ -r CHANGES ] ; then
	more CHANGES
	echo $n "[Enter to begin]"
	read cc
   fi
fi

clear

#
# Take a wild stab at the OS, and take reasonable defaults for each
#
# Include zlib as default for server compression, 
# if they don't want it, they have a backspace key.
# patched: 5/30/00 Paul de Regt [Ax0n] (deregt@accessnw.net)

OS=`uname -a`
case "$OS" in
    *NetBSD*)
	DEFCFLAGS="$DEFOPT"
	DEFLIBS="-lcrypt -lz"
	OSNAME="NetBSD"
	;;
    *FreeBSD*)
	DEFCFLAGS="$DEFOPT"
	DEFLIBS="-lcrypt -lz"
	OSNAME="FreeBSD"
	;;
    *SCO_SV*)
	DEFCFLAGS="$DEFOPT -DSCOUNIX"
	DEFLIBS="-lsocket -lz"
	OSNAME="SCO Openserver"
	NEEDSKIPNAME="Yep"
	;;
    *OSF1*alpha*)
	DEFCFLAGS="$DEFOPT"
	DEFLIBS="-lz"
	OSNAME="OSF/1 or Digital Unix"
	;;
    *SunOS*4.*)
	DEFCFLAGS="$DEFOPT"
	DEFLIBS="-lz"
	OSNAME="SunOS 4.x"
        LOCALD=undef
	;;
    *SunOS*5.*)
	DEFCFLAGS="$DEFOPT -DSOL20"
	DEFLIBS="-lsocket -lnsl -lresolv -lz"
	OSNAME="Solaris 2.x (or SunOS 5.x)"
        LOCALD=undef
	;;
    *Darwin*)
	DEFCFLAGS="$DEFOPT"
	DEFLIBS="-lz"
	OSNAME="Darwin"
	;;
    *Linux*)
# New, cleaner detection code. -8/15/99 Sean-Paul Rees (sean@dreamfire.net)
	cat > $TMP << __EOF__
#include <stdio.h>
main()
{
	#ifdef __GLIBC__
	printf("%d_%d\n", __GLIBC__, __GLIBC_MINOR__);
	#else
	printf("0\n");
	#endif

	exit(0);
}
__EOF__
#
# Yes, I know we don't have $CCPATH and $XCFLAGS and such set up yet,
# but below should work on just about any Linux system
#
# Glibc version now correctly detected (major.minor)
# patched: 8/15/99 Sean-Paul Rees (sean@dreamfire.net)
#
	cc $TMP -o $EXEC >/dev/null 2>&1
	GLIBCVERS=`$EXEC`

	case $GLIBCVERS in
	    0)	# This is Linux without Glibc.
	        OSNAME="Linux"
                DEFLIBS="-lz"
                ;;
            2*) # Linux with Glibc 2.x
                OSNAME="Linux (Glibc $GLIBCVERS)"
                DEFLIBS="-lcrypt -lresolv -lz"
                ;;
        esac	
		
	DEFCFLAGS="$DEFOPT"
	;;
    *HP-UX*.09.*)
	DEFCFLAGS="$DEFOPT"
	DEFLIBS="-lz"
	OSNAME="HPUX 9.x"
	;;
    *HP-UX*.10.*)
	DEFCFLAGS="$DEFOPT"
	DEFLIBS="-lz"
	OSNAME="HPUX 10.x"
	;;
esac

echo " "
echo Operating System: $OSNAME
echo " "
	
# Create Makefile if it doesn't exist...
if [ ! -f Makefile ] ; then
  cp Makefile.dist Makefile
fi
cat << __EOF__
Welcome to autoconfigure for the AstralIRCd server version $IRCD_VERSION.

Config will generate a system-specific $SETUP file, a top
level Makefile and a persistant options file named
$OPTIONS

Enter "none" at any prompt to effect a null entry.

__EOF__

runonce=""
FOO=`egrep "^CC=" Makefile 2>/dev/null | sed -e 's/^[^=]*[ 	]*=\(.*\)/\1/'`
while [ -z "$CCPATH" ] ; do
	MYP=`echo "$PATH" | sed -e 's/:/ /g'`
	echo "Which compiler do you use, gcc or cc?"
	echo $n "[$FOO] -> $c"
	if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	    read cc
	else
	    cc=""
	fi
	if [ -z "$cc" ] ; then
		cc=$FOO
		CCPATH=$FOO
	elif [ -f $cc ] ; then
		CCPATH=$cc
	else
		for i in $MYP; do
			if [ -f $i/$cc -a -z "$CCPATH" ] ; then
				CCPATH=$i/$cc
			fi
		done
	fi
	if [ -z "$CCPATH" ]; then runonce="Yes"; fi
done
if [ "$FOO" != "$cc" ] ; then
	MYP=`echo "$CCPATH" | sed -e 's@/@ @g'`
	set $MYP
	if [ $2 ] ; then
		while [ $2 ] ; do
			shift
		done
	fi
	if [ $1 = "gcc" ] ; then
		CCPATH="$CCPATH"
	fi
fi
echo "Compiler selected: $CCPATH"
echo " "
# Check it out
cat > $TMP <<__EOF__
main() {}
__EOF__
$CCPATH $TMP -o $EXEC >/dev/null 2>&1
if [ ! -f $EXEC ] ; then
        echo "You don't have $CCPATH or it's broken."
        exit 1
fi
# Fix Makefile
#
$RM -f Makefile.tmp
sed -e "s@^CC=\(.*\)@CC=$CCPATH@" Makefile > Makefile.tmp
cp Makefile.tmp Makefile
$RM -f Makefile.tmp
#
echo "Enter additional flags to give to $CCPATH"
FOO=`egrep "^XCFLAGS=" Makefile 2>/dev/null | sed -e 's/^[^=]*=[	 ]*\(.*\)/\1/'`
if [ -z "$FOO" ] ; then
    echo "I recommend $DEFCFLAGS"
    FOO="$DEFCFLAGS"
fi
echo $n "[$FOO] -> $c"
if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
    read cc
else
    cc=""
fi
if [ -z "$cc" ] ; then
	cc="$FOO"
fi
if [ "$cc" = "none" ] ; then
	cc=''
fi
XCFLAGS=$cc
# Fix Makefile
#
$RM -f Makefile.tmp
sed -e "s@^XCFLAGS=\(.*\)@XCFLAGS=$XCFLAGS@" Makefile > Makefile.tmp
cp Makefile.tmp Makefile
$RM -f Makefile.tmp
#
cat <<__EOF__

If you need to use any extra libraries when compiling the server,
please tell me now and please include all the -l and -L flags.

You should use the recommended value unless you have a compelling reason
not to...
__EOF__
LIBS=`egrep "^IRCDLIBS=" Makefile 2>/dev/null | sed -e 's/^[^=]*=\(.*\)/\1/' | tr -d "\012"`
if [ -z "$LIBS" ] ; then
    echo "I suggest: $DEFLIBS"
    LIBS="$DEFLIBS"
fi
echo $n "[$LIBS] -> $c"
if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
    read cc
else
    cc=""
fi
if [ -z "$cc" ] ; then
	cc="$LIBS"
fi
if [ "$cc" = "none" ] ; then
	cc=''
fi
LIBS=$cc
# Fix Makefile
#
$RM -f Makefile.tmp
sed -e "s@^IRCDLIBS=\(.*\)@IRCDLIBS=$LIBS@" Makefile > Makefile.tmp
cp Makefile.tmp Makefile
$RM -f Makefile.tmp
#
COMP="$CCPATH $XCFLAGS $TMP -o $EXEC $LIBS"
#
echo 'Checking out /usr/include'
echo $n "Looking for /usr/include/stdlib.h...$c"
if [ -r /usr/include/stdlib.h ] ; then
	STDLIBH=define
	echo  'found'
else
	echo 'not found'
fi
echo $n "Looking for stddef.h...$c"
if [ -r /usr/include/stddef.h ] ; then
        STDDEFH=define
	echo  'found'
else
	echo 'not found'
fi
echo $n "Looking for /usr/include/sys/syslog.h...$c"
if [ -r /usr/include/sys/syslog.h ] ; then
	SYSSYSLOGH=define
	echo  'found'
else
	echo 'not found'
fi

echo $n "Looking for /usr/include/sys/param.h...$c"
if [ -r /usr/include/sys/param.h ] ; then
	PARAMH=define
	echo  'found'
else
	echo 'not found'
fi
echo $n "Looking for /usr/include/unistd.h...$c"
if [ -r /usr/include/unistd.h ] ; then
	UNISTDH=define
	echo  'found'
else
	echo 'not found'
fi
echo $n "Looking for /usr/include/string.h...$c"
if [ -r /usr/include/string.h ] ; then
	STRINGH=define
	echo  'found'
else
	echo 'not found'
fi
echo $n "Looking for /usr/include/strings.h...$c"
if [ -r /usr/include/strings.h ] ; then
	STRINGSH=define
	echo  'found'
else
	echo 'not found'
fi
echo $n "Looking for /usr/include/sys/rusage.h...$c"
if [ -r /usr/include/sys/rusage.h ] ; then
	RUSAGEH=define
	echo  'found'
else
	echo 'not found (good)'
fi
#
# to b or not to b
#
echo $n "Searching for bcopy/bzero/bcmp...$c"
cat > $TMP <<__EOF__
main()
{
	char	a[3], b[3];
	bzero(b,3);
	bcopy(a,b,3);
	(void)bcmp(a,b,3);
	exit(0);
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -eq 0 ] ; then
	echo "located bcopy/bzero/bcmp"
	HAVE_BZERO=define
else
	echo "No bcopy/bzero/bcmp found"
	HAVE_BZERO=undef
fi
echo " "
echo $n "Which one, gettimeofday, or lrand48..$c"
cat > $TMP <<__EOF__
#include <stdio.h>
#include <sys/time.h>
main()
 {
   struct timeval tv;
   (void) gettimeofday(&tv, NULL);
 }
__EOF__
$COMP >/dev/null 2>&1
if [ $? -eq 0 ] ; then
	echo "gettimeofday found"
	GETTIMEOFDAY=define
else
	echo "No gettimeofday. Trying lrand48."
cat > $TMP <<__EOF__
main()
{
   int a;
   a=lrand48();
}
__EOF__
	$COMP >/dev/null 2>&1
	if [ $? -eq 0 ] ; then
 		echo "lrand48 found"
		LRAND48=define
	fi
fi
#
# check for non-blocking fd style available..
#
echo 'Checking for POSIX/BSD/SYSV'
if [ -f $TMP -o -d $TMP ] ; then
	$RM -f $TMP
fi
cat > $PLATE <<__EOF__
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <signal.h>
alarmed()
{
	exit(1);
}
main()
{
	char b[12], x[32];
	int f, l = sizeof(x);
	f = socket(AF_INET, SOCK_DGRAM, 0);
	if (f >= 0 && !(fcntl(f, F_SETFL, BLOCKING))) {
		signal(SIGALRM, alarmed);
		alarm(3);
		recvfrom(f, b, 12, 0, x, &l);
		alarm(0);
		exit(0);
	}
	exit(1);
}
__EOF__
sed -e 's/BLOCKING/O_NONBLOCK/' $PLATE > $TMP
$COMP >/dev/null 2>&1
if [ 0 -eq $? ] ; then
	$EXEC
fi
if [ 0 -eq $? ] ; then
	POSIX_NBLOCK=define
else
	echo 'O_NONBLOCK not present/working in fcntl.h or sys/ioctl.h'
	if [ -f $TMP -o -d $TMP ] ; then
		$RM -f $TMP $EXEC;
	fi
	sed -e 's/BLOCKING/O_NDELAY/' $PLATE > $TMP
	$COMP >/dev/null 2>&1
	if [ 0 -eq $? ] ; then
		$EXEC
	fi
	if [ 0 -eq $? ] ; then
		BSD_NBLOCK=define
	else
		echo 'O_NDELAY not present/working in fcntl.h or sys/ioctl.h'
		if [ -f $TMP -o -d $TMP ] ; then
			$RM -f $TMP $EXEC;
		fi
		sed -e 's/BLOCKING/FIONBIO/' $PLATE > $TMP
		$COMP >/dev/null 2>&1
		if [ 0 -eq $? ] ; then
			echo 'ERROR: FIONBIO not found!  No option found!'
		else
			echo "Using SysV blocking"
		fi
	fi
fi
$RM -f $TMP $PLATE $EXEC
echo "Blocking selected: $BLOCK";
#
# reliable signals ?
#
echo 'Looking for reliable signals...'
echo "Checking if you have 'action from POSIX..."
cat > $TMP <<__EOF__
#include <signal.h>

main()
{	/* poor replacement for NULL but who cares here ? */
	sigaction(SIGTERM, (struct sigaction *)0L, (struct sigaction *)0L);
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -eq 0 ] ; then
	echo "'action from POSIX found"
	POSIX_SIGNAL=define
else
	$RM -f $EXEC $TMP
	cat > $TMP <<__EOF__
#include <signal.h>
int	calls = 0;
void	handler()
{
	if (calls)
		return;
	calls++;
	kill(getpid(), SIGTERM);
	sleep(1);
}
main()
{
	signal(SIGTERM, handler);
	kill(getpid(), SIGTERM);
	exit (0);
}
__EOF__
	echo $n "No, but you have...$c"
	$COMP >/dev/null 2>&1
	$EXEC
	if [ $? -eq 0 ] ; then
		echo 'reliable signals found'
		BSD_SIGNAL=define
	else
		echo "unreliable SYSV detected"
	fi
fi
$RM -f $EXEC $TMP
#
echo 'Now those strings libraries...'
cat > $TMP <<__EOF__
#$STRINGH STRINGH
#$STRINGSH STRINGSH
#ifdef STRINGH
#include <string.h>
#endif
#ifdef STRINGSH
#include <strings.h>
#endif
main()
{
	char *s = index("foo", 'o');
	exit(0);
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -eq 0 ] ; then
	echo "you have index()!"
        HINDEX=define
else
	echo "I guess there is a strchr() out there somewhere..."
fi
$RM -f $EXEC $TMP
#
# getrusage or times ?
#
echo $n "One for debugging, mainly, getrusage(2) or times(2)...$c"
cat > $TMP <<__EOF__
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
main()
{
	struct	rusage	rus;
	(void)getrusage(RUSAGE_SELF, &rus);
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -eq 0 ] ; then
	GETRUSAGE_2=define
	echo "getrusage()"
else
	$RM -f $EXEC $TMP
	cat > $TMP <<__EOF__
#include <sys/types.h>
#include <sys/time.h>
#include <sys/times.h>
main()
{
	struct	tms	tmsbuf;
	(void)times(&tmsbuf);
}
__EOF__
	$COMP >/dev/null 2>&1
	if [ $? -eq 0 ] ; then
		TIMES_2=define
		echo "times()"
	else
		echo "couldn't get either ?!"
	fi
fi
#
# what do we need that isn't here already ?
#
echo -n "Looking for strcasecmp..."
cat > $TMP <<__EOF__
#$STRINGH STRINGH
#$STRINGSH STRINGSH
#ifdef STRINGH
#include <string.h>
#endif
#ifdef STRINGSH
#include <strings.h>
#endif
main()
{
        char *s = strcasecmp('a', 'a');
        exit(0);
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -eq 0 ] ; then
        echo "strcasecmp found"
        STRCASECMP=define
else
        echo "No strcasecmp found"
fi
#$RM -f $EXEC $TMP
echo "Checking for additional components..."
echo $n "Searching...$c"
cat > $TMP <<__EOF__
main()
{
	unsigned long foo;

	char  *s = strtoul("0x12345", &foo, 16);
	exit(0);
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -ne 0 ] ; then
    echo $n " strtoul$c"
    $RM -f Makefile.tmp
    sed -e "s@^STRTOUL=\(.*\)@STRTOUL=strtoul.o@" Makefile > Makefile.tmp
    cp Makefile.tmp Makefile
    $RM -f Makefile.tmp
else
    $RM -f Makefile.tmp
    sed -e "s@^STRTOUL=\(.*\)@STRTOUL=@" Makefile > Makefile.tmp
    cp Makefile.tmp Makefile
    $RM -f Makefile.tmp
fi
$RM -f $EXEC $TMP
cat > $TMP <<__EOF__
main()
{
	char  *s = strerror(0);
	exit(0);
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -ne 0 ] ; then
	echo $n " strerror$c"
else
	STRERROR=define
fi
$RM -f $EXEC $TMP
cat > $TMP <<__EOF__
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>

main()
{
	dn_skipname("","");
	exit(0);
}
__EOF__
$COMP >/dev/null 2>&1
# dn_skipname passes the small test but won't resolve when ircd is linked.
# Hmmm...
if [ $? -ne 0 -o "$OSNAME" = "Linux RedHat 5.0" -o -n "$NEEDSKIPNAME" -o "$GLIBCCHECK" = "define" ] ; then
    echo $n " dn_skipname$c"
    $RM -f Makefile.tmp
    sed -e "s@^RES=\(.*\)@RES=res_skipname.o@" Makefile > Makefile.tmp
    cp Makefile.tmp Makefile
    $RM -f Makefile.tmp
else
    $RM -f Makefile.tmp
    sed -e "s@^RES=\(.*\)@RES=res_init.o@" Makefile > Makefile.tmp
    cp Makefile.tmp Makefile
    $RM -f Makefile.tmp
fi
$RM -f $EXEC $TMP
cat > $TMP <<__EOF__
#include <sys/types.h>
main()
{
    u_int32_t foo;
    exit(0);
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -ne 0 ] ; then
    echo $n " u_int32_t$c"
    $RM -f Makefile.tmp
    sed -e "s@^NEED_U_INT32_T=\(.*\)@NEED_U_INT32_T=-DNEED_U_INT32_T@" Makefile > Makefile.tmp
    cp Makefile.tmp Makefile
    $RM -f Makefile.tmp
else
    $RM -f Makefile.tmp
    sed -e "s@^NEED_U_INT32_T=\(.*\)@NEED_U_INT32_T=@" Makefile > Makefile.tmp
    cp Makefile.tmp Makefile
    $RM -f Makefile.tmp
fi
$RM -f $EXEC $TMP
cat > $TMP <<__EOF__
#$STRINGH STRINGH
#$STRINGSH STRINGSH
#ifdef STRINGH
#include <string.h>
#endif
#ifdef STRINGSH
#include <strings.h>
#endif
main()
{
	char  t[] = "a", **p = NULL, *s = strtoken(&p, t, ",");
	if (!strcmp(t, s))
		exit(0);
	exit(1);
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -ne 0 ] ; then
	echo $n " strtoken$c"
	STRTOKEN=undef
else
	$EXEC
	if [ $? -ne 0 ] ; then
		echo $n " strtoken$c"
		STRTOKEN=undef
	fi
fi
$RM -f $EXEC $TMP
cat > $TMP <<__EOF__
#$STRINGH STRINGH
#$STRINGSH STRINGSH
#ifdef STRINGH
#include <string.h>
#endif
#ifdef STRINGSH
#include <strings.h>
#endif
main()
{
	char  t[] = "a", *s = strtok(t, ",");
	if (!strcmp(t, s))
		exit(0);
	exit(1);
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -ne 0 ] ; then
	echo $n " strtok$c"
	STRTOK=undef
else
	$EXEC
	if [ $? -ne 0 ] ; then
		echo $n " strtok$c"
		STRTOK=undef
	fi
fi
$RM -f $EXEC $TMP
cat > $TMP << __EOF__
#include <sys/types.h>
#include <netinet/in.h>
main()
{
	struct	in_addr in;
	(void)inet_addr("1.2.3.4");
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -ne 0 ] ; then
	echo $n " inet_addr$c"
	INETADDR=undef
fi
$RM -f $EXEC $TMP
cat > $TMP << __EOF__
#include <sys/types.h>
#include <netinet/in.h>
main()
{
	struct	in_addr	in;
	in.s_addr = 0x12345678;
	(void)inet_ntoa(in);
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -ne 0 ] ; then
	echo $n " inet_ntoa$c"
fi
$RM -f $EXEC $TMP
cat > $TMP << __EOF__
#include <sys/types.h>
#include <netinet/in.h>
main()
{
	struct	in_addr	in;
	in.s_addr = 0x87654321;
	(void)inet_netof(in);
}
__EOF__
$COMP >/dev/null 2>&1
if [ $? -ne 0 ] ; then
	echo $n " inet_netof$c"
	INETNETOF=undef
fi
$RM -f $EXEC $TMP
echo " "

$RM -f $EXEC $TMP $PLATE
cat > $SETUP  <<__EOF__
#define IRCD_VERSION "$IRCD_VERSION"
#$PARAMH	HAVE_PARAM_H
#$UNISTDH	HAVE_UNISTD_H
#$STRINGH	HAVE_STRING_H
#$STRINGSH	HAVE_STRINGS_H
#$STDLIBH	HAVE_STDLIB_H
#$STDDEFH	HAVE_STDDEF_H
#$SYSSYSLOGH	HAVE_SYSSYSLOG_H
#$HINDEX	HAVE_INDEX
#$STRERROR	HAVE_STRERROR
#$STRTOKEN	HAVE_STRTOKEN
#$STRTOK	HAVE_STRTOK
#$INETADDR	HAVE_INET_ADDR
#$INETNTOA	HAVE_INET_NTOA
#$INETNETOF	HAVE_INET_NETOF
#$GETTIMEOFDAY	GETTIMEOFDAY
#$LRAND48	HAVE_LRAND48
#$STRCASECMP	HAVE_STRCASECMP
#$HAVE_BZERO	HAVE_BZERO
#$POSIX_NBLOCK	HAVE_POSIX_NBLOCK
#$BSD_NBLOCK	HAVE_BSD_NBLOCK
#$POSIX_SIGNAL	HAVE_POSIX_SIGNALS
#$BSD_SIGNAL	HAVE_BSD_SIGNALS
#$TIMES_2	HAVE_TIMES_2
#$GETRUSAGE_2	HAVE_GETRUSAGE_2
__EOF__

if [ $? -ne 0 -o "$OSNAME" = "Darwin" ] ; then  
echo "#define __Darwin__" >> $SETUP
fi

#
# Now, get site specific options.
#

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$KLINE_ADDRESS"
    echo " "
    echo "What is the contact address for connect problems due to the"
    echo "user being K:lined, shown to the user when they attempt to"
    echo "connect?  This should be a valid email address."
    echo " "
    echo "Note that this message is displayed when"
    echo "the user is affected by a local K:line or k:line. "
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" -o -z "$FOO" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	*@*.*)
	    KLINE_ADDRESS=$cc
	    FOO="$KLINE_ADDRESS"
	    ;;
	*)
	    echo " "
	    echo "Read the instructions and try again...  You did not enter a"
	    echo "proper email address (user@host.domain)."
	    FOO=""
	    ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$DPATH"
    echo ""
    echo "What directory are all the server configuration files in?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
done
DPATH=$cc

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$DPATH/ircd"
    echo ""
    echo "What is the explicit path to where the ircd binary will be"
    echo "installed?  This should point to a file, not a directory"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
done
SPATH=$cc

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    echo ""
    echo "What is your local domain name?  This is used for /stats w to"
    echo "report local clients vs. remote clients."
    if [ "$LOCALD" = "define" ] ; then
      echo "Auto-probing local host..."
      HOSTNAME=`hostname`
      if [ $? -eq 0 ]; then
          TEMPHOST="${HOSTNAME#*.}"
        if [ "${#HOSTNAME}" -ne "${#TEMPHOST}" ]; then
          FOO="${TEMPHOST}"
        else 
          echo ""
          echo "This guess probably isn't right:"
          FOO="${HOSTNAME}"
        fi
      else
        FOO="$DOMAINNAME"
      fi
    else
      echo "Skipping auto-probe. (Bad OS: $OSNAME)"
      FOO="configure.this"
    fi
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    FOO=$cc
done
DOMAINNAME=$cc
   
FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    if [ -n "$HUB" ] ; then
	FOO="Yes"
    else
	FOO="No"
    fi
    echo ""
    echo "Are you running as a HUB server?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[Yy]*)
	    HUB="1"
	    ;;
	[Nn]*)
	    HUB=""
	    ;;
	*)
	    echo ""
	    echo "You need to enter either Yes or No here..."
	    echo ""
	    FOO=""
	    ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    if [ -n "$CRYPT_OPER_PASSWORD" ] ; then
	FOO="Yes"
    else
	FOO="No"
    fi
    echo ""
    echo "Do you use encrypted operator passwords?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[Yy]*)
	    CRYPT_OPER_PASSWORD="1"
	    ;;
	[Nn]*)
	    CRYPT_OPER_PASSWORD=""
	    ;;
	*)
	    echo ""
	    echo "You need to enter either Yes or No here..."
	    echo ""
	    FOO=""
	    ;;
    esac
done
CRYPT_ILINE_PASSWORD=$CRYPT_OPER_PASSWORD

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    if [ -n "$ZIP_LINKS" ] ; then
	FOO="Yes"
    else
	FOO="No"
    fi
    echo ""
    echo "Do you want to enable server compression?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[Yy]*)
	    ZIP_LINKS="1"
	    ;;
	[Nn]*)
	    ZIP_LINKS=""
	    ;;
	*)
	    echo ""
	    echo "You need to enter either Yes or No here..."
	    echo ""
	    FOO=""
	    ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$MAXCONNECTIONS"
    echo ""
    echo "How many file descriptors (or sockets) can the irc server use?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[1-9][0-9][0-9]*)
	    MAXCONNECTIONS="$cc"
	    ;;
	*)
	    echo ""
	    echo "You need to enter a number here, greater or equal to 100."
	    echo ""
	    FOO=""
	    ;;
    esac
done

#
# check FD_SETSIZE and override if needed.
#

cat > $TMP <<__EOF__
#include <stdio.h>
#include <sys/types.h>

#ifndef FD_SETSIZE
#define FD_SETSIZE -1
#endif

/*
 * Prints "notdef" if FD_SETSIZE is undefined,
 *        "ok"     if FD_SETSIZE is at least as large as provided on the
 *                 compile command line (-DMAXCONNECTIONS=1234)
 *        "###"    if it is less.  (### is the FD_SETSIZE value)
 */
int
main(int argc, char *argv[])
{
	if (FD_SETSIZE == -1)
		printf("notdef\n");
	else if (FD_SETSIZE >= MAXCONNECTIONS)
		printf("ok\n");
	else
		printf("%d\n", FD_SETSIZE);

	return 0;
}
__EOF__
$COMP "-DMAXCONNECTIONS=$MAXCONNECTIONS" >/dev/null 2>&1
if [ $? -ne 0 ] ; then
    echo " "
    echo "I could not derrive what your system allows for the maximum number"
    echo "of connections becuase the test program did not compile."
    echo " "
    FD_SETSIZE=""
else
    fd_setsize_ok=`$EXEC`
    case $fd_setsize_ok in
	notdef)
	    echo " "
	    echo "I could not derrive what your system allows for the maximum"
	    echo "number of connections because the test program did not find"
	    echo "a system-supplied value for FD_SETSIZE.  Assuming it is"
	    echo "defined correctly but the test program cannot find it."
	    echo " "
	    FD_SETSIZE=""
	    ;;
	ok)
	    echo " "
	    echo "Your system-supplied value for FD_SETSIZE is large enough"
	    echo "for ircd to leave it untouched."
	    echo " "
	    FD_SETSIZE=""
	    ;;
	*)
	    echo " "
	    echo "Your system-supplied value for FD_SETSIZE is $fd_setsize_ok"
	    echo "but you requested $MAXCONNECTIONS for ircd.  FD_SETSIZE will"
	    echo "be overridden using -DFD_SETSIZE=$MAXCONNECTIONS when"
	    echo "compiling ircd."
	    echo " "
	    FD_SETSIZE=$MAXCONNECTIONS
	    ;;
    esac
fi

if [ -n "$FD_SETSIZE" ] ; then
    $RM -f Makefile.tmp
    sed -e "s@^FD_SETSIZE=\(.*\)@FD_SETSIZE=-DFD_SETSIZE=$FD_SETSIZE@" Makefile > Makefile.tmp
    cp Makefile.tmp Makefile
    $RM -f Makefile.tmp
else
    $RM -f Makefile.tmp
    sed -e "s@^FD_SETSIZE=\(.*\)@FD_SETSIZE=@" Makefile > Makefile.tmp
    cp Makefile.tmp Makefile
    $RM -f Makefile.tmp
fi

$RM -f $EXEC $TMP


cat <<__EOF__

The file "$OPTIONS" was either created or rewritten to contain your
answers to the above questions.

This file is automatically generated and will be updated each time you
run Config.  You should retain a copy of this to help migrate to future
versions of the KT-IRCd server with ease.

__EOF__

#
# continue the $SETUP file
#
cat >> $SETUP << __EOF__
#define DPATH "$DPATH"
#define SPATH "$SPATH"
#define MAXCONNECTIONS $MAXCONNECTIONS
__EOF__
if [ -n "$HUB" ] ; then
    echo "#define HUB" >> $SETUP 
else
    echo "#undef HUB" >> $SETUP
fi
if [ -n "$PARANOIA" ] ; then
    echo "#define PARANOIA" >> $SETUP 
else
    echo "#undef PARANOIA" >> $SETUP
fi
if [ -n "$CRYPT_OPER_PASSWORD" ] ; then
    echo "#define CRYPT_OPER_PASSWORD 1" >> $SETUP
else
    echo "#undef CRYPT_OPER_PASSWORD" >> $SETUP
fi
if [ -n "$CRYPT_ILINE_PASSWORD" ] ; then
    echo "#define CRYPT_ILINE_PASSWORD 1" >> $SETUP
else
    echo "#undef CRYPT_ILINE_PASSWORD" >> $SETUP
fi
if [ -n "$CONTACT_URL" ] ; then
    echo "#define CONTACT_URL \"$CONTACT_URL\"" >> $SETUP
fi
if [ -n "$CONTACT_EMAIL" ] ; then
    echo "#define CONTACT_EMAIL \"$CONTACT_EMAIL\"" >> $SETUP
fi
if [ -n "$SERVICES_NAME" ] ; then
    echo "#define SERVICES_NAME \"$SERVICES_NAME\"" >> $SETUP
fi
if [ -n "$ASTRAL_NAME" ] ; then
    echo "#define ASTRAL_NAME \"$ASTRAL_NAME\"" >> $SETUP
fi
if [ -n "$KLINE_ADDRESS" ] ; then
    echo "#define KLINE_ADDRESS \"$KLINE_ADDRESS\"" >> $SETUP
fi
if [ -n "$DOMAINNAME" ] ; then
    echo "#define DOMAINNAME \"$DOMAINNAME\"" >> $SETUP
else
    echo "#undef DOMAINNAME" >> $SETUP
fi
if [ -n "$ZIP_LINKS" ] ; then
    echo "#define ZIP_LINKS	1" >> $SETUP
else 
    echo "#undef ZIP_LINKS" >> $SETUP
fi

#
# Create file for make install
#
rm -f $MAKEINST
cat > $MAKEINST << __EOF__

if [ ! -d $DPATH ] ; then
    mkdir $DPATH
fi

if [ -f $SPATH ] ; then
    cp $SPATH $SPATH.old
fi

if [ -f $DPATH/ircd.pid ] ; then
    kill -9 \`cat $DPATH/ircd.pid \` 
fi

cp src/ircd $SPATH
$SPATH
__EOF__
#
chmod 711 $MAKEINST
#
# create the persistant file
#
rm -f $OPTIONS
cat > $OPTIONS << __EOF__
#
# VERSION: $IRCD_VERSION
# DATE:    $CONF_DATE
#
# This file is automatically generated and will be updated each time you
# run Config.  You should retain a copy of this to help migrate to future
# versions of the KT-IRCd server with ease.
#
LAST_VERSION="$IRCD_VERSION"
CONTACT_URL="$CONTACT_URL"
CONTACT_EMAIL="$CONTACT_EMAIL"
SERVICES_NAME="$SERVICES_NAME"
ASTRAL_NAME="$ASTRAL_NAME"
KLINE_ADDRESS="$KLINE_ADDRESS"
ZIP_LINKS="$ZIP_LINKS"
DPATH="$DPATH"
SPATH="$SPATH"
HUB="$HUB"
PARANOIA="$PARANOIA"
CRYPT_OPER_PASSWORD="$CRYPT_OPER_PASSWORD"
MAXCONNECTIONS="$MAXCONNECTIONS"
DOMAINNAME="$DOMAINNAME"
__EOF__

cat <<__EOF__

 Config is complete.

 Before compiling, check the contents of include/config.h for any strange
 things you usually set.  If you want them to be included in the future,
 ask them to be moved to the setup.h file on coding@xivservers.net.

__EOF__
